解题思路:
第一处:num初始化错误,应为:num[k]=0;。
第二处:由于s是指针型变量,所以应改为:switch(*s)。
***************************************************
请编写函数 fun,函数的功能是求出二维数组周边元素之和,作为函数值返回。二维数组中的值在主函数中赋予。
例如:二维数组中的值为
1 3 5 7 9
2 9 9 9 4
6 9 9 9 8
1 3 5 7 0
则函数值为 61。
注意: 部分源程序存在文件PROG1.C文件中。
请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。
给定源程序:
#include
#define M 4
#define N 5
int fun ( int a[M][N] )
{
}
main( )
{ int aa[M][N]={{1,3,5,7,9},
{2,9,9,9,4},
{6,9,9,9,8},
{1,3,5,7,0}};
int i, j, y;
clrscr();
printf ( "The original data is : \n" );
for ( i=0; i { for ( j =0; j printf ("\n");
}
y = fun ( aa );
printf( "\nThe sum: %d\n" , y );
printf("\n");
NONO( );
}
解题思路:
本题是统计二维数组周边元素值之和,但要注意的是不要重复计算四个角上的元素值,结果作为函数值返回。
参考答案:
int fun ( int a[M][N] )
{
int tot = 0, i, j ;
for(i = 0 ; i < N ; i++) {
tot += a[0][i] ;
tot += a[M-1][i] ;
}
for(i = 1 ; i < M - 1 ; i++) {
tot += a[i][0] ;
tot += a[i][N-1] ;
}
return tot ;
}